package com.hgy.crowd.service.impl;

import com.hgy.crowd.mapper.*;
import com.hgy.crowd.po.*;
import com.hgy.crowd.service.ProjectService;
import com.hgy.crowd.util.ResultEntity;
import com.hgy.crowd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author He
 * @Date 2020/7/4 19:10
 */
@Service
@Transactional(readOnly = true)
public class ProjectServiceImpl implements ProjectService {
    @Resource
    private ProjectPOMapper projectPOMapper;
    @Resource
    private ProjectItemPicPOMapper projectItemPicPOMapper;
    @Resource
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;
    @Resource
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;
    @Resource
    private ReturnPOMapper returnPOMapper;
    @Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @Override
    public ResultEntity<String> saveProjectVORemote(ProjectVO tempProject, Integer memberId) {
        ProjectPO projectPO=new ProjectPO();
        BeanUtils.copyProperties(tempProject,projectPO);
        projectPO.setMemberid(memberId);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String createDate = simpleDateFormat.format(new Date());
        projectPO.setCreatedate(createDate);
        projectPOMapper.insertSelective(projectPO);
        //获取自增的主键
        Integer projectId = projectPO.getId();
        projectPOMapper.insertRelationship(tempProject.getTypeIdList(),projectId);

        projectPOMapper.insertTagRelationShip(tempProject.getTagIdList(),projectId);
        List<String> detailPicturePathList = tempProject.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(detailPicturePathList,projectId);

        MemberLaunchInfoVO memberLauchInfoVO = tempProject.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO=new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO,memberLaunchInfoPO);
        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        List<ReturnVO> returnVOList = tempProject.getReturnVOList();
        List<ReturnPO> returnPOList=new LinkedList<>();
        for (ReturnVO returnVO:returnVOList){
            ReturnPO returnPO=new ReturnPO();
            BeanUtils.copyProperties(returnVO,returnPO);
            returnPOList.add(returnPO);
        }
        returnPOMapper.insertReturnPOList(returnPOList,projectId);

        MemberConfirmInfoVO memberConfirmInfoVO = tempProject.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO=new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO,memberConfirmInfoPO);
        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);

        return ResultEntity.successWithoutData();
    }
    @Override
    public List<PortalTypeVO> getPortalTypeVO() {
        return projectPOMapper.selectPortalTypeVOList();
    }

    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {
        // 1.查询得到 DetailProjectVO 对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        // 2.根据 status 确定 statusText
        Integer status = detailProjectVO.getStatus();
        switch (status) {
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;
            default:
                break;
        }
        // 3.根据 deployeDate 计算 lastDay
        // 2020-10-15
        String deployDate = detailProjectVO.getDeployDate();
        // 获取当前日期
        Date currentDay = new Date();
        // 把众筹日期解析成 Date 类型
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date deployDay = format.parse(deployDate);
            // 获取当前当前日期的时间戳
            long currentTimeStamp = currentDay.getTime();
            // 获取众筹日期的时间戳
            long deployTimeStamp = deployDay.getTime();
            // 两个时间戳相减计算当前已经过去的时间
            long pastDays = (currentTimeStamp - deployTimeStamp) / 1000 / 60 / 60 / 24;
            // 获取总的众筹天数
            Integer totalDays = detailProjectVO.getDay();
            // 使用总的众筹天数减去已经过去的天数得到剩余天数
            Integer lastDay = (int) (totalDays - pastDays);
            detailProjectVO.setLastDay(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return detailProjectVO;
    }
}
